package com.junzhiit.nengji.app.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bainaeco.framework.util.AssertUtil;
import com.junzhiit.nengji.app.service.TransactionService;
import com.junzhiit.nengji.app.web.vo.ResultVO;
import com.junzhiit.nengji.app.web.vo.TransactionAnalysisVO;
import com.junzhiit.nengji.app.web.vo.TransactionInfoVO;
import com.junzhiit.nengji.model.enums.AnalysisType;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("transaction")
public class TransactionController extends BaseController {

	@Autowired
	private TransactionService transactionService;

	@ApiOperation(value = "获取所有交易分析数据")
	@RequestMapping(value="/analysis",method = RequestMethod.GET)
	public ResultVO getTransactionAnalysis(String sessionId,Integer analysisType,String date,String startTime,String endTime) {
		AssertUtil.notBlank(sessionId, "sessionId is required");
		AssertUtil.notNull(analysisType, "analysisType is required");
		if(analysisType==AnalysisType.DAY.toIntValue()||analysisType==AnalysisType.MONTH.toIntValue()){
			AssertUtil.notBlank(date, "date is required");
		} else if(analysisType==AnalysisType.CUSTOM.toIntValue()){
			AssertUtil.notBlank(startTime, "startTime is required");
			AssertUtil.notBlank(endTime, "endTime is required");
		}
		TransactionAnalysisVO transactionAnalysisVO=transactionService.getTransactionAnalysis(sessionId,analysisType,date,startTime,endTime);
		TransactionInfoVO transactionInfoVO= transactionService.findAllTransactionAnalysis(sessionId,analysisType,date,startTime,endTime);
		ResultVO result=new ResultVO();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("transactionAnalysis", transactionAnalysisVO);
		map.put("transactionInfo", transactionInfoVO);
		result.setData(map);
		return result;
	}
	
	@ApiOperation(value="获取品牌所有交易分析数据")
	@RequestMapping(value="/analysis/chain",method=RequestMethod.GET)
	public ResultVO getChainTransactionAnalysis(String sessionId,Integer analysisType,String date,String startTime,String endTime,String shopId){
		AssertUtil.notBlank(sessionId, "sessionId is required");
		AssertUtil.notNull(analysisType, "analysisType is required");
		if(analysisType==AnalysisType.DAY.toIntValue()||analysisType==AnalysisType.MONTH.toIntValue()){
			AssertUtil.notBlank(date, "date is required");
		} else if(analysisType==AnalysisType.CUSTOM.toIntValue()){
			AssertUtil.notBlank(startTime, "startTime is required");
			AssertUtil.notBlank(endTime, "endTime is required");
		}
		TransactionAnalysisVO transactionAnalysisVO=transactionService.getChainTransactionAnalysis(sessionId,analysisType,date,startTime,endTime,shopId);
		TransactionInfoVO transactionInfoVO= transactionService.findAllChainTransactionAnalysis(sessionId,analysisType,date,startTime,endTime,shopId);
		ResultVO result=new ResultVO();
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("transactionAnalysis", transactionAnalysisVO);
		map.put("transactionInfo", transactionInfoVO);
		result.setData(map);
		return result;
	}
}
